home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / binaries / Windows / jsdk / src / sun / servlet / isapi / Servlet.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-18  |  4.0 KB  |  167 lines

  1. /*
  2.  * @(#)Servlet.cpp    1.7 97/05/14
  3.  * 
  4.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.0
  20.  */
  21.  
  22. #include <stdio.h>
  23. #include <HttpExt.h>
  24. #include <jni.h>
  25.  
  26. #include <io.h>
  27.  
  28. #undef AFX_DATA
  29. #define AFX_DATA AFX_EXT_DATA
  30.  
  31. #define BUFLEN 1024
  32.  
  33. //static JDK1_1InitArgs vmArgs;
  34. static JavaVM *vmPtr;
  35. static JNIEnv *env = NULL;
  36.  
  37. static char PreAmble[] = "<body><h1>";
  38. static char PostAmble[] = "</h1></body>";
  39.  
  40. char *CLASSPATH, *MAINCLASS, *PATH, *PROPFILE;
  41.  
  42. int getRegistryValues();
  43.  
  44. void clobberClassPath(JDK1_1InitArgs *args) {
  45.     putenv("CLASSPATH=");
  46.     putenv("JAVA_HOME=");
  47.     putenv(PATH);
  48.     args->classpath = CLASSPATH;
  49. }
  50.  
  51. DWORD SendError( EXTENSION_CONTROL_BLOCK *pEcb, const char *msg, DWORD status )
  52.  {
  53.      char buf[BUFLEN];
  54.      int preLen = strlen(PreAmble);
  55.      int postLen = strlen(PostAmble);
  56.      int msgLen = strlen(msg);
  57.      strcpy(pEcb->lpszLogData, msg);
  58.      pEcb->dwHttpStatusCode = status;
  59.      strcpy(buf, PreAmble);
  60.      int toWrite = (msgLen > BUFLEN - preLen - postLen) ?
  61.          BUFLEN - preLen - postLen : msgLen;
  62.  
  63.      strncpy(buf+preLen, msg, toWrite);
  64.      strcpy(buf+preLen+msgLen, PostAmble);
  65.      DWORD len = strlen(buf);
  66.  
  67.      pEcb->WriteClient(pEcb->ConnID, buf, &len, HSE_IO_SYNC);
  68.  
  69.      return HSE_STATUS_SUCCESS;
  70.  }
  71.  
  72. BOOL WINAPI GetExtensionVersion( HSE_VERSION_INFO *pVer ) 
  73. {
  74.     JDK1_1InitArgs vmArgs;
  75.     jclass mainClass;
  76.  
  77.     pVer->dwExtensionVersion = MAKELONG( HSE_VERSION_MINOR,
  78.                          HSE_VERSION_MAJOR );
  79.     lstrcpyn( pVer->lpszExtensionDesc,
  80.               "Java Servlet Runner",
  81.               HSE_MAX_EXT_DLL_NAME_LEN );
  82.  
  83.     if (env != NULL) {
  84.         return TRUE;
  85.     }
  86.  
  87.     getRegistryValues();
  88.  
  89.     JNI_GetDefaultJavaVMInitArgs(&vmArgs);
  90.  
  91.     // clobber the classpath
  92.     clobberClassPath(&vmArgs);
  93.  
  94.     // create the VM
  95.     if (JNI_CreateJavaVM(&vmPtr, &env, &vmArgs)) {
  96.         return FALSE;
  97.     }
  98.  
  99.     // find the main class
  100.     mainClass = env->FindClass(MAINCLASS);
  101.     if (mainClass == NULL) {
  102.         return FALSE;
  103.     }
  104.     
  105.     // find the static void main(String[] arg) method
  106.     jmethodID mID = env->GetStaticMethodID(mainClass,
  107.                            "main",
  108.                            "([Ljava/lang/String;)V");
  109.     if (mID == NULL) {
  110.         return FALSE;
  111.     }
  112.  
  113.     // execute the main method
  114.     jclass strClass = env->FindClass("java/lang/String");
  115.     jobjectArray jargs = env->NewObjectArray(1, strClass, 0);
  116.     char buf[256];
  117.     sprintf(buf, "servlet.propfile=\0");
  118.     strcat(buf, PROPFILE);
  119.     jstring str = env->NewStringUTF(buf);
  120.     env->SetObjectArrayElement(jargs, 0, str);
  121.     env->CallStaticVoidMethod(mainClass, mID, jargs);
  122.  
  123.     jthrowable exc = env->ExceptionOccurred();
  124.     if (exc) {
  125.         return FALSE;
  126.     }
  127.  
  128.     return TRUE;
  129. }
  130.  
  131. DWORD WINAPI HttpExtensionProc( EXTENSION_CONTROL_BLOCK *pEcb )
  132. {
  133.     jclass mainClass;
  134.     jmethodID serviceMethod;
  135.  
  136.     // find the main class
  137.     mainClass = env->FindClass(MAINCLASS);
  138.     if (mainClass == NULL) {
  139.         return HSE_STATUS_ERROR;
  140.     }
  141.  
  142.     // find the static void handleConnection(long) method
  143.     serviceMethod = env->GetStaticMethodID(mainClass,
  144.                            "handleConnection",
  145.                            "(J)V");
  146.  
  147.     if (serviceMethod == NULL) {
  148.         return HSE_STATUS_ERROR;
  149.     }
  150.  
  151.     //jstring ret = (jstring)env->CallStaticObjectMethod(mainClass, serviceMethod, pEcb);
  152.     //const char *bbb = env->GetStringUTFChars(ret, 0);
  153.     //env->ReleaseStringUTFChars(ret, bbb);
  154.  
  155.     env->CallStaticVoidMethod(mainClass, serviceMethod, pEcb);
  156.  
  157.     jthrowable exc = env->ExceptionOccurred();
  158.     if (exc) {
  159.         return HSE_STATUS_ERROR;
  160.     }
  161.      return HSE_STATUS_SUCCESS;
  162. }
  163.  
  164.  
  165. #undef AFX_DATA
  166. #define AFX_DATA
  167.